package cn.levi.sensitive.configuration;

import cn.hutool.dfa.WordTree;
import cn.levi.sensitive.component.ActionIfHasSensitiveWord;
import cn.levi.sensitive.component.ActionReplaceSensitiveWord;
import cn.levi.sensitive.component.SensitiveFactory;
import cn.levi.sensitive.component.SensitiveHandler;
import java.util.Set;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author lei
 * @since 2025/2/18
 */

@Configuration
@Import(SensitiveProperties.class)
public class SensitiveConfiguration {



	@ConditionalOnMissingBean
	@Bean
	@ConditionalOnProperty(prefix = "simple-tool.sensitive", name = "enable", havingValue = "true", matchIfMissing = true)
	@ConditionalOnBean(SensitiveFactory.class)
	public SensitiveHandler getSensitiveHandler(SensitiveFactory sensitiveFactory,@Qualifier("SensitiveRedisTemplate") RedisTemplate<String, Object> redisTemplate) {
		SensitiveHandler sensitiveHandler = new SensitiveHandler(redisTemplate, sensitiveFactory);
		sensitiveHandler.refreshSensitiveWord();
		return sensitiveHandler;
	}
	/**
	 * 获取敏感词树处理程序
	 *
	 * @param sensitiveFactory 敏感工厂
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnBean(value = {SensitiveFactory.class,SensitiveProperties.class})
	@ConditionalOnProperty(prefix = "simple-tool.sensitive",name = "enable",havingValue = "true",matchIfMissing = true)
	public ActionIfHasSensitiveWord getActionIfHasSensitiveWord(SensitiveHandler sensitiveHandler,SensitiveProperties sensitiveFactory) {
		ActionIfHasSensitiveWord actionIfHasSensitiveWord = new ActionReplaceSensitiveWord(sensitiveHandler,sensitiveFactory);
		return actionIfHasSensitiveWord;
	}
	@Bean(name = "SensitiveRedisTemplate")
	@ConditionalOnMissingBean
	@ConditionalOnBean(value = {SensitiveFactory.class,SensitiveProperties.class})
	@ConditionalOnProperty(prefix = "simple-tool.sensitive",name = "enable",havingValue = "true",matchIfMissing = true)
	public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory){
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(factory);
		// 使用 JSON 序列化
		Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
		template.setDefaultSerializer(serializer);
		// String 类型的 key 序列化
		template.setKeySerializer(new StringRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		return template;
	}


}
